package com.express.gateway.spi.proxy.custom;

import java.util.ArrayList;
import java.util.List;

import com.express.gateway.common.util.StrUtil;
import com.express.gateway.manage.entity.Api;
import com.express.gateway.spi.proxy.custom.impl.GetConstantValueProxyHandler;
import com.express.gateway.spi.proxy.custom.impl.GetServerUnixTimestampProxyHandler;
import com.express.gateway.spi.proxy.custom.impl.SessionTokenGrantAuthProxyHandler;

import io.vertx.core.http.HttpClient;
import io.vertx.core.json.JsonObject;

/**
 * 自定义处理器工厂
 *
 * @author flower
 */
public class CustomHandlerFactory {
    /**
     * 获得VxApi当前服务的unix时间戳
     */
    public static String GET_SERVER_UNIX_TIMESTAMP = "GET_SERVER_UNIX_TIMESTAMP";
    /**
     * 获得自定义常量
     */
    public static String GET_CONSTANT_VALUE = "GET_CONSTANT_VALUE";
    /**
     * session_token权限认证的授权
     */
    public static String SESSION_TOKEN_GRANT_AUTH = "SESSION_TOKEN_GRANT_AUTH";

    /**
     * 获得所有实现类名字
     *
     * @return
     */
    public static List<String> getImplNames() {
        List<String> result = new ArrayList<>();
        result.add(GET_SERVER_UNIX_TIMESTAMP);
        result.add(GET_CONSTANT_VALUE);
        result.add(SESSION_TOKEN_GRANT_AUTH);
        return result;
    }

    /**
     * 自定义处理器
     *
     * @param name       处理器在工厂中的名字
     * @param options    处理器配置文件
     * @param api        API 相关配置文件
     * @param httpClient http的客户端
     * @return CustomHandler
     * @throws NullPointerException
     * @throws ClassNotFoundException
     * @throws Exception
     */
    public static CustomProxyHandler getCustomHandler(String name, JsonObject options, Api api, HttpClient httpClient) throws NullPointerException, ClassNotFoundException, Exception {
        if (StrUtil.isNullOrEmpty(name)) {
            throw new NullPointerException("获取自定义处理器 失败:工厂名字不能为空");
        }
        if (SESSION_TOKEN_GRANT_AUTH.equalsIgnoreCase(name)) {
            return new SessionTokenGrantAuthProxyHandler(options, api, httpClient);
        }
        if (GET_SERVER_UNIX_TIMESTAMP.equalsIgnoreCase(name)) {
            return new GetServerUnixTimestampProxyHandler(options);
        }
        if (GET_CONSTANT_VALUE.equalsIgnoreCase(name)) {
            return new GetConstantValueProxyHandler(options);
        }
        throw new ClassNotFoundException("没有找到名字为 : " + name + " 的实现类");
    }

}
